package com.example.bailin.giftsay.login;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseActivity;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by bailin on 16/9/1.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivRegisterClose;
    private EditText etRegisterUsername;
    private EditText etRegisterPass;
    private Button btnRegister;

    @Override
    protected int setLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        etRegisterUsername = bindView(R.id.et_register_username);
        etRegisterPass = bindView(R.id.et_register_pass);
        ivRegisterClose = bindView(R.id.iv_register_close);
        btnRegister = bindView(R.id.btn_register);
        ivRegisterClose.setOnClickListener(this);
        btnRegister.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        Bmob.initialize(this, "d56d6973b86cc9cf2a71774f4f16ccfe");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_register_close:
                finish();
                break;

            case R.id.btn_register:
                final String uN = handle(etRegisterUsername.getText().toString());
                final String pW = handle(etRegisterPass.getText().toString());


                if (uN.equals("")) {
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                } else if (pW.equals("")) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else {
                    BmobUser bmobUser = new BmobUser();
                    bmobUser.setUsername(uN);
                    bmobUser.setPassword(pW);

                    bmobUser.signUp(new SaveListener<BmobUser>() {
                        @Override
                        public void done(BmobUser bmobUser, BmobException e) {
                            if (e == null) {
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent();
                                intent.putExtra("username", uN);
                                intent.putExtra("passwork", pW);
                                setResult(RESULT_OK, intent);
                                finish();
                            } else {
                                Log.d("RegisterActivity", e.toString());
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                etRegisterUsername.setText(null);
                                etRegisterPass.setText(null);
                            }
                        }
                    });
                }


                break;
        }
    }


    private String handle(String content) {
        return content.replace(" ", "");
    }
}
